package com.znsd.dining.room.utils;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.util.StringUtils;
import com.znsd.dining.room.response.ResultData;
import org.apache.commons.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName FileSheetUtils
 * @Description: 数据表格导入导出
 * @Author timi
 * @Date 2020/1/2
 * @Time 16:24
 * @Version V1.0
 **/
public class FileSheetUtils {
    
    private static Logger logger = LoggerFactory.getLogger(FileSheetUtils.class);
    
    private static Sheet sheet;
    private final static Integer SHEET_NO = 0;
    private static String filePath = "F:\\test";
    private static String suffixFile = ".xlsx";
    /**
     * @Author timi
     * @Description //指定对象导出
     * @Date 17:07 2020/1/2
     * @Params
     * @return
     */
    public static ResultData deriverData(String fileName, Class<? extends BaseRowModel> clazz, List<? extends BaseRowModel> data){
        getSheet();
        if (null == clazz){
            return  null;
        }
        sheet.setSheetName(fileName);
        sheet.setClazz(clazz);
        return  deriverFile(fileName, sheet, data)? null : null;
    }
    /**
     * @Author timi
     * @Description //指定读出头部的
     * @Date 17:14 2020/1/2
     * @Params
     * @return
     */
    public static ResultData deriverData(String fileName, List<? extends BaseRowModel> data, String... heads){
        getSheet();
        if (StringUtils.isEmpty(heads) || heads.length == SHEET_NO) {
            return ResultData.error();
        }
        sheet.setSheetName(fileName);
        List<List<String>> tableHeads = new ArrayList<>();
        Arrays.asList(heads).forEach( h -> tableHeads.add(Collections.singletonList(h)));
        sheet.setHead(tableHeads);
        return  deriverFile(fileName, sheet, data)? ResultData.ok() : ResultData.error();
    }
    
    private static boolean  deriverFile(String fileNaem, Sheet sheet, List<? extends BaseRowModel> data){
        
        OutputStream outputStream = null;
        ExcelWriter writer = null;
        // 拼接文件路径
        StringBuffer path = new StringBuffer(filePath).append("\\").append(fileNaem).append(suffixFile);
        try {
            outputStream = new FileOutputStream(path.toString());
            writer = EasyExcelFactory.getWriter(outputStream);
            writer.write(data,sheet);
            return true;
        }catch (FileNotFoundException e) {
            logger.info("出现错误了");
            return  false;
        }finally {
            writer.finish();
            try{
                outputStream.close();
            }catch (IOException e) {
                logger.info("关闭 outPutStream 出错了");
            }
        }
    }
    
    /**
     * @Author timi
     * @Description //读取xmls文件数据
     * @Date 11:14 2020/1/3
     * @Params
     * @return
     */
    public static boolean readSheet(String filePath){
        getSheet();
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(filePath);
            List<Object> read = EasyExcelFactory.read(inputStream, sheet);
            logger.info("查询出来的数据为："+read);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    /**
     * @Author timi
     * @Description //创建 并设置 sheet对象的宽度
     * @Date 17:14 2020/1/2
     * @Params
     * @return
     */
    private static void getSheet(){
        if (null == sheet) {
            sheet = new Sheet(SHEET_NO,0);
            // 设置自适应宽带
            sheet.setAutoWidth(Boolean.TRUE);
        }
    }
}
